package com.mobjam.workingtitleforagame;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class StoreActivity extends Activity {
	
	private SharedPreferences prefs;
	private String prefName = "MyPref";
	private static final String CURRENCY_VALUE_KEY  = "Gold";
	
	private RelativeLayout storeRelativeLayout;
	
	private Button buyButton;
	private Button cancelButton;
	
	private TextView itemName;
	private TextView itemType;
	private TextView itemSlot;
	private TextView itemStat;
	private TextView itemModify;
	private TextView itemCost;
	private TextView currency;
	
	private int gold;
	private int cost;
	
	TypedArray item_name;
	TypedArray item_type;
	TypedArray item_slot;
	TypedArray item_stat;
	TypedArray item_modify;
	TypedArray item_cost;
	TypedArray item_icon;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    // TODO Auto-generated method stub
	    setContentView(R.layout.activity_sale);
	    
	    cost = 0;
	    
	    item_name = getResources().obtainTypedArray(R.array.item_name);
	    item_type = getResources().obtainTypedArray(R.array.item_type);
	    item_slot = getResources().obtainTypedArray(R.array.item_slot);
	    item_stat = getResources().obtainTypedArray(R.array.item_stat);
	    item_modify = getResources().obtainTypedArray(R.array.item_modify);
	    item_cost = getResources().obtainTypedArray(R.array.item_cost);
	    item_icon = getResources().obtainTypedArray(R.array.item_icon);
	    
	    int data_size = item_name.length();
	    
	    Item[] item_data = new Item[data_size];
	    
	    for (int i=0; i < data_size; i++){
	    	item_data[i] = new Item(item_name.getString(i),
	    							item_type.getString(i),
	    							item_slot.getString(i),
	    							item_stat.getString(i),
	    							item_modify.getInteger(i, 0),
	    							item_cost.getInteger(i, 0),
	    							item_icon.getDrawable(i));
	    }
	    
	    storeRelativeLayout = (RelativeLayout) findViewById(R.id.storeRelativeLayout);
	    storeRelativeLayout.setBackgroundColor(Color.LTGRAY);
	    
	    Gallery storeGallery = (Gallery) findViewById(R.id.gallery1);
	    storeGallery.setBackgroundColor(Color.GRAY);
	    storeGallery.setAdapter(new StoreGalleryAdapter(this, item_data));
	    storeGallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				// TODO Auto-generated method stub
				ImageView imageView = (ImageView) findViewById(R.id.imageView1);
				imageView.setImageDrawable(item_icon.getDrawable(position));
				
				itemName = (TextView) findViewById(R.id.itemNameTextView);
				itemName.setText(item_name.getString(position));
				
				itemType = (TextView) findViewById(R.id.itemTypeTextView);
				itemType.setText("Type: " + item_type.getString(position));
				
				itemSlot = (TextView) findViewById(R.id.itemSlotTextView);
				itemSlot.setText("Slot: " + item_slot.getString(position));
				
				itemStat = (TextView) findViewById(R.id.itemStatTextView);
				itemStat.setText("Stat: " + item_stat.getString(position));
				
				itemModify = (TextView) findViewById(R.id.itemModTextView);
				itemModify.setText("Modify: " + item_modify.getInt(position, 0));
				
				itemCost = (TextView) findViewById(R.id.itemCostTextView);
				itemCost.setText("Cost: " + item_cost.getInt(position, 0));
				
				cost = item_cost.getInt(position, 0);
			}
	    });
	    
	    buyButton = (Button) findViewById(R.id.button3);
	    buyButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				//---get the SharedPreferences object---
				//prefs = getSharedPreferences(prefName, MODE_PRIVATE);
				//SharedPreferences.Editor editor = prefs.edit();
				
				//---save the value in the Textview to preferences---
				gold = gold - cost;
				Character.getInstance().setGold(gold);
				currency.setText("Gold: " + gold);
				//editor.putInt(CURRENCY_VALUE_KEY, gold);
				
				//---save the values---
				//editor.commit();
			}
		});
	    
	    cancelButton = (Button) findViewById(R.id.button4);
	    cancelButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	    
	    //---load the SharedPReferences object---
	    //SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
	    
	    //---set the TextView currency to the previously saved value---
	    gold = Character.getInstance().getGold(); //prefs.getInt(CURRENCY_VALUE_KEY, 1000);
	    currency = (TextView) findViewById(R.id.goldTextView1);
	    currency.setText("Gold: " + gold);
	}

}
